package com.csii.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.autopoi.poi.excel.entity.ExportParams;
import com.csii.autopoi.web.def.NormalExcelConstants;
import com.csii.core.annotation.AutoLog;
import com.csii.core.api.Result;
import com.csii.core.system.base.controller.BaseController;
import com.csii.core.system.query.QueryGenerator;
import com.csii.core.system.vo.LoginUser;
import com.csii.core.util.ConvertUtils;
import com.csii.core.util.oConvertUtils;
import com.csii.user.controller.vo.SysRoleGroupRelationVO;
import com.csii.user.entity.SysPermission;
import com.csii.user.entity.SysRoleGroup;
import com.csii.user.entity.SysRoleGroupRelation;
import com.csii.user.model.TreeModel;
import com.csii.user.service.ISysPermissionService;
import com.csii.user.service.ISysRoleGroupRelationService;
import com.csii.user.util.PermissionExcelView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: sys_role_group_relation
 * @Author: power-stdio
 * @Date:   2021-04-27
 * @Version: V1.0
 */
@Api(tags="角色组关系")
@RestController
@RequestMapping("/sys/roleGroupRelation")
@Slf4j
public class SysRoleGroupRelationController extends BaseController<SysRoleGroupRelation, ISysRoleGroupRelationService> {
	@Autowired
	private ISysRoleGroupRelationService sysRoleGroupRelationService;
	/**
	 * 分页列表查询
	 *
	 * @param sysRoleGroupRelationVO
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="sys_role_group_relation-分页列表查询", notes="sys_role_group_relation-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SysRoleGroupRelationVO sysRoleGroupRelationVO,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		SysRoleGroupRelation sysRoleGroupRelation  = ConvertUtils.convert(SysRoleGroupRelation.class,sysRoleGroupRelationVO);
		QueryWrapper<SysRoleGroupRelation> queryWrapper = QueryGenerator.initQueryWrapper(sysRoleGroupRelation, req.getParameterMap());
		Page<SysRoleGroupRelation> page = new Page<SysRoleGroupRelation>(pageNo, pageSize);
		IPage<SysRoleGroupRelation> pageList = sysRoleGroupRelationService.page(page, queryWrapper);
		IPage<SysRoleGroupRelationVO> pageListVo = ConvertUtils.convertIPage(SysRoleGroupRelationVO.class, pageList);
		return Result.ok(pageListVo);
	}

	 @ApiOperation(value="sys_role_group_relation-查询", notes="sys_role_group_relation-查询")
	 @GetMapping(value = "/queryByGroupId")
	 public Result<?> queryByGroupId(@RequestParam  String groupId) {
		 List<SysRoleGroupRelation> pageListVo=sysRoleGroupRelationService.lambdaQuery()
				 .eq(SysRoleGroupRelation::getGroupId,groupId).list();
		 return Result.ok(pageListVo.stream().map(a->a.getRoleId()));
	 }
	@ApiOperation(value="sys_role_group_relation-查询", notes="sys_role_group_relation-查询")
	@GetMapping(value = "/saveBatch")
	 public Result<?> saveBatch(@RequestParam String groupId,@RequestParam String roleIds){
		List<String> roleIdList= Arrays.asList(roleIds.split(","));
		sysRoleGroupRelationService.saveBatch(groupId,roleIdList);
		return Result.ok();
	 }
	/**
	 *   添加
	 *
	 * @param sysRoleGroupRelationVO
	 * @return
	 */
	@AutoLog(value = "sys_role_group_relation-添加")
	@ApiOperation(value="sys_role_group_relation-添加", notes="sys_role_group_relation-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysRoleGroupRelationVO sysRoleGroupRelationVO) {
		SysRoleGroupRelation sysRoleGroupRelation  = ConvertUtils.convert(SysRoleGroupRelation.class,sysRoleGroupRelationVO);
		sysRoleGroupRelationService.save(sysRoleGroupRelation);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param sysRoleGroupRelationVO
	 * @return
	 */
	@AutoLog(value = "sys_role_group_relation-编辑")
	@ApiOperation(value="sys_role_group_relation-编辑", notes="sys_role_group_relation-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysRoleGroupRelationVO sysRoleGroupRelationVO) {
		SysRoleGroupRelation sysRoleGroupRelation  = ConvertUtils.convert(SysRoleGroupRelation.class,sysRoleGroupRelationVO);
		sysRoleGroupRelationService.updateById(sysRoleGroupRelation);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "sys_role_group_relation-通过id删除")
	@ApiOperation(value="sys_role_group_relation-通过id删除", notes="sys_role_group_relation-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		sysRoleGroupRelationService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "sys_role_group_relation-批量删除")
	@ApiOperation(value="sys_role_group_relation-批量删除", notes="sys_role_group_relation-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysRoleGroupRelationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="sys_role_group_relation-通过id查询", notes="sys_role_group_relation-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SysRoleGroupRelation sysRoleGroupRelation = sysRoleGroupRelationService.getById(id);
		if(sysRoleGroupRelation==null) {
			return Result.error("未找到对应数据");
		}
		SysRoleGroupRelationVO sysRoleGroupRelationVO  = ConvertUtils.convert(SysRoleGroupRelationVO.class,sysRoleGroupRelation);
		return Result.ok(sysRoleGroupRelationVO);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysRoleGroupRelationVO
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysRoleGroupRelationVO sysRoleGroupRelationVO) {
        SysRoleGroupRelation sysRoleGroupRelation  = ConvertUtils.convert(SysRoleGroupRelation.class,sysRoleGroupRelationVO);
        return super.exportXls(request, sysRoleGroupRelation, SysRoleGroupRelation.class, "sys_role_group_relation");
    }

}
